// 地图
dingwei()
function dingwei() {
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12);

    // 新建一个地图辅助定位对象
    var geolocation = new BMap.Geolocation();
    // 开启SDK辅助定位(如果不给系统定位权限,误差比较大)
    geolocation.enableSDKLocation();
    // 读取当前位置
    geolocation.getCurrentPosition(function (r) {
        // 回调的参数是位置信息
        console.log(r)
        // 回调中的this指的是定位对象
        console.log(this, geolocation)
        // 获取定位状态
        console.log(geolocation.getStatus(), BMAP_STATUS_SUCCESS)
        // 通过某种定义,使一个字符变量对应一个数字, 这种数据结构叫 '枚举'
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            // 创建地图标注点
            var mk = new BMap.Marker(r.point);
            // 把标注点添加到地图上
            map.addOverlay(mk);
            // 把地图中心移动到当前标注点
            map.panTo(r.point);
            // alert('您的位置：'+r.longitude+','+r.latitude);

            // 逆地址解析: 把经纬度转成省市县街道
            // 创建地理编码实例, 并配置参数获取乡镇级数据
            var myGeo = new BMap.Geocoder({ extensions_town: true });
            // 根据坐标得到地址描述    
            myGeo.getLocation(r.point, function (result) {
                if (result) {
                    alert(result.address);
                    document.querySelector(".currentboxText").innerText = result.address
                }
            });
        }
        else {
            alert('failed' + this.getStatus());
        }
    });

}


var activity = document.querySelectorAll(`.activity`);
// 活动 随机背景色
window.onload = function () {
    var spans = document.querySelectorAll(`.activity  p span:nth-child(1)`)
    console.log(spans);
    for (let index = 0; index < spans.length; index++) {
        i = index
        console.log(i);
        var activity = document.querySelectorAll(`.activity`)
        for (let j = 0; j < activity.length; j++) {
            if (activity[j].getElementsByTagName("p")[i]) {
                var pSpans = activity[j].getElementsByTagName("p")[i].getElementsByTagName("span")[0]
                console.log(activity[j].getElementsByTagName("p")[i]);
                var r = Math.round(Math.random() * 255)
                var g = Math.round(Math.random() * 255)
                var b = Math.round(Math.random() * 255)
                // var pSpans = document.querySelector(`.activity  p:nth-child(${i}) span:nth-child(1)`)
                pSpans.style.backgroundColor = `rgb(${r},${g},${b})`
            } else { break }
        }
    }
}

// location start
var locations = document.querySelector(".location")

// 选收货地址
function locationIndex() {
    locations.style.left = 0
    locations.style.transition = ".5s";
    locations.style.width = "30rem"
}
// 收货地址返回
function locationIndexTo() {
    locations.style.left = "30rem"
    locations.style.transition = ".5s";
    locations.style.width = "0rem"
    document.querySelector(".addressNew").style.display = "none"
}
var addressNewid = document.querySelector("#addressNewid")
addressNewid.onclick = function () {
    document.querySelector(".addressNew").style.display = "block"
    document.getElementById("addressNewMaininp").value = ""
    document.getElementById("tcl").value = ""
    document.getElementById("xiaoqu").value = ""
    document.getElementById("lou").value = ""
}
// 性别
function addressButton() {
    // alert(document.querySelector(".addressNan"));
    document.querySelector(".addressNan").classList.toggle('acolor')
    document.querySelector(".addressNv").classList.toggle('acolor')
}
// 添加地址
var addressNewENT = document.querySelector(".addressNewENT")
addressNewENT.onclick = function () {
    if (document.getElementById("addressNewMaininp").value == "" || document.getElementById("tcl").value == "" || document.getElementById("xiaoqu").value == "" || document.getElementById("lou").value == "") {
        console.log("信息未填满")
    }
    else {
        var inputname = document.getElementById("addressNewMaininp").value
        var acolor = document.getElementsByClassName("acolor")[0].innerText
        var tcl = document.getElementById("tcl").value
        var xiaoqu = document.getElementById("xiaoqu").value
        var lou = document.getElementById("lou").value
        var address = document.querySelector(".address")
        var node = document.createElement('li')
        node.innerHTML = `
    <p><b>${inputname}</b> <span>${acolor}</span> <span>${tcl}</span> </p>
    <p>${xiaoqu} ${lou}</p>`
        address.appendChild(node)
    }
    document.querySelector(".addressNew").style.display = "none"
}

// 
// location end
// 创建地图实例 start
var map = new BMap.Map("container");
function myFun(result) {
    var cityName = result.name;
    map.setCenter(cityName);
    // alert("当前定位城市:" + cityName);
    container.innerText = cityName
}
var myCity = new BMap.LocalCity();
myCity.get(myFun);
// 创建地图实例 end





// window.navigator.geolocation.getCurrentPosition(function (result) {
//     console.log(result.coords)
//     // 经度: result.coords.longitude   纬度: result.coords.latitude
// })

// 窗口下拉 返回顶部
window.onwheel = function () {
    if (window.scrollY > 20) {
        document.querySelector("header").classList.add("topFixed")
        document.querySelector("header h1").style.display = "none"
        // console.log(window.scrollY);
        document.querySelector(".shangjt").style.display = "block"
    } else {
        show()
    }
}
// 返回顶部
function xTop() {
    document.body.scrollTop = document.documentElement.scrollTop = 0;
    show()
}
// 显示
function show() {
    document.querySelector("header").classList.remove("topFixed")
    document.querySelector("header h1").style.display = "block"
    document.querySelector(".shangjt").style.display = "none"
}
